using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.Networking;
using UnityEngine.Playables;
using UnityEngine.U2D;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace Framework
{
    public class AssetTypeUtil
    {
        /// <summary>
        /// 图片后缀
        /// </summary>
        public static readonly string[] textureExts = { ".jpg", ".png", ".tga", ".exr" };
        public static readonly string matExt = ".mat";
        public static readonly string[] gameObjExts = { ".fbx", ".prefab" };
        public static readonly string animatorExt = ".controller";
        public static readonly string[] txtExts = { ".txt", ".xml", ".json" };
        public static readonly string[] audioExts = { ".mp3", ".wav" };
        public static readonly string spriteAtlasExt = ".spriteatlas";
        public static readonly string timelineExt = ".playable";
        public static readonly string animationExt = ".anim";

        /// <summary>
        /// 从文件的路径后缀来判定文件类型
        /// </summary>
        /// <param name="assetPath"></param>
        /// <returns></returns>
        internal static System.Type TypeFromExt(string assetPath)
        {
            int index = assetPath.LastIndexOf(".");
            if (index <= -1)
            {
                return null;
            }

            string extenstion = assetPath.Substring(index).ToLower();
            foreach (string texExt in textureExts)
            {
                if (texExt.Equals(extenstion))
                {
                    return typeof(Texture);
                }
            }

            if (matExt.Equals(extenstion))
            {
                return typeof(Material);
            }

            foreach (string goExt in gameObjExts)
            {
                if (goExt.Equals(extenstion))
                {
                    return typeof(GameObject);
                }
            }

            if (extenstion.Equals(animatorExt))
            {
                return typeof(RuntimeAnimatorController);
            }

            foreach (string txtExt in txtExts)
            {
                if (txtExt.Equals(extenstion))
                {
                    return typeof(TextAsset);
                }
            }
            foreach (string audioExt in audioExts)
            {
                if (audioExt.Equals(extenstion))
                {
                    return typeof(AudioClip);
                }
            }

            if (spriteAtlasExt.Equals(extenstion))
            {
                return typeof(SpriteAtlas);
            }

            if (timelineExt.Equals(extenstion))
            {
                return typeof(PlayableAsset);
            }

            if (animationExt.Equals(extenstion))
            {
                return typeof(AnimationClip);
            }

            //SLLogger.LogWarning("TypeFromExt.TypeFromExt, can not check type for assetPath = " + assetPath);
            return null;
        }

        internal static System.Type GetType(string assetPath)
        {
            if (getFromOthers != null)
            {
                var type = getFromOthers(assetPath);
                if (type != null)
                {
                    return type;
                }
            }

            return AssetTypeUtil.TypeFromExt(assetPath);
        }

        public static Func<string, System.Type> getFromOthers;
    }
}